<?php	
/*
	PookiWiki - A wiki unlike the rest.
	Copyright (C) 2012 Jordan Craw

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
	
	class PookiHTMLElement
	{
		public $attributes;
		public $classes;
		public $id;
		public $contenteditable = FALSE;
		public $contextmenus; // Not yet implemented
		public $text_direction;
		public $is_draggable = FALSE;
		public $is_drop_zone = FALSE;
		public $is_hidden = FALSE;
		public $is_spellchecked = FALSE;
		public $language; // Here's something I want to implement quickly.
		public $mouseover_title;
		
		private $style;
		private $tab_index;
		
		public $unsettable_variables = array(
			"classes",
			"id",
		);
		
		public function setBaseAttributes()
		{
				$this->attributes = array(
					"content-editable" => $this->contenteditable ? "TRUE" : "FALSE",
					"contextmenu"  => is_null($this->contextmenus) ? "default" : $this->contextmenus,
					"dir" => in_array($this->text_direction,array("ltr","rtl")) ? $this->text_direction : "ltr",
					"lang" => is_null($this->language)? "en-GB" : $this->language
				);
		}
	}



	class PookiHTMLGenerator
	{
		protected $available_elements = array(
			"html" =>array(
				"head" => array(
					"title","base","link","meta","style","script"
				),
				"body" => array(
						"a","abbr","address","area","article","aside","audio",
						"b","base","bdi","bdo","blockquote","body","br","button",
						"canvas","caption","cite","code","col","colgroup","command",
						"data","datagrid","datalist","dd","del","details","dfn","div",
						"dl","dt","em","embed","eventsource","fieldset","figcaption",
						"figure","footer","form","h1","h2","h3","h4","h5","h6","head",
						"header","hgroup","hr","i","iframe","img","input","ins","kbd",
						"keygen","label","legend","li","link","mark","map","menu","meta",
						"meter","nav","noscript","object","ol","optgroup","option","output",
						"p","param","pre","progress","q","ruby","rp","rt","s","samp","script",
						"section","select","small","source","span","strong","style","sub",
						"summary","sup","table","tbody","td","textarea","tfoot","th","thead",
						"time","title","tr","track","u","ul","var","video","wbr"
				)
			)
		);
			
		private function generateElementProperties($element_properties)
		{
			if(is_array($element_properties))
			{
				$property_count = count(array_keys($element_properties));
				
				if($property_count == 1)
				{
					$HTML = " " . (string) array_pop(array_keys($element_properties)) . "='";
					$HTML .= array_pop(array_values($element_properties)) . "' ";
					return " " . $HTML . " ";
				} 
				elseif($property_count> 1) 
				{ 
					// I have to say, I hate these self-aggrandizing comments, but by nature, this code is sexy.
					$HTML = implode("=\"%s\" ",array_keys($element_properties)) . "=\"%s\"";
					$HTML = vsprintf($HTML,array_values($element_properties));
					return " " . $HTML . " ";
				}
				else
				{
					return "";
				}
			}
			else
			{
				return FALSE;
			}
		}
		
		static function generateElement($element_name,$element_properties,$terminates = FALSE, $content = "")
		{
			$generator = new PookiHTMLGenerator();
			
			$is_here = array_search($element_name,$generator->available_elements);
			
			if(in_array($element_name,$generator->available_elements["html"]["body"]) || in_array($element_name,$generator->available_elements["html"]["title"]))
			{
				$HTML = "<" . $element_name . $generator->generateElementProperties($element_properties);
				$HTML .= $terminates? "/>" : ">" . $content . "</" . $element_name . ">";
				unset($generator);
				return $HTML;
			}
			else
			{
				return FALSE;	
			}
		}
	}
	
?>